package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.ErrorCode
import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ErrorReadCapEntity
import com.hzjq.core.callback.Callback
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work

/**
 * 授权进度查询
 */
class AuthQueryWork : Work<CapProgressEntity> {

    private var progress = 0

    private var authErrorCall : ProgressCallback<ErrorReadCapEntity>?=null

    private var mAuthErrorReadCapWork:AuthErrorReadCapWork?=null

    constructor(callback: Callback<CapProgressEntity>?,authErrorCall : ProgressCallback<ErrorReadCapEntity>?) : super(callback){
        this.authErrorCall = authErrorCall
    }

    override fun doWork(vararg args: Any) {
        if (args.isNotEmpty()) {
            progress = args[0] as Int
            onProgressChanged(progress, "正在检测授权信息")
            Receives.getInstance()
                .registerReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader()
                    .getAuthCycleQueryCmd(),
                    object : Receiver {
                        override fun convert(msg: String): Any {
                            return BlastDelegate.getDelegate().getParseLoader()
                                .parseScanProgress(msg)
                        }

                        override fun onSuccess(msg: Any) {
                            if (msg is CapProgressEntity) {
                                if (msg.stateCode == 0) {
                                    callback?.onResult(msg)
                                    if (msg.progress < 100) {
                                        progress = 1 + msg.progress * 50 / 100
                                        retry(progress)
                                    } else {
                                        doNext(progress)
                                    }
                                } else {
                                    if(msg.stateCode == 3){
                                        onProgressChanged(progress, "没有检测到雷管反馈")
                                        callback?.onError(ErrorCode.getErrorResult(3))
                                        onDestroy()
                                    } else {
                                        onProgressChanged(progress, "检测授权信息失败")
                                        mAuthErrorReadCapWork = AuthErrorReadCapWork(authErrorCall)
                                        mAuthErrorReadCapWork?.cancel()
                                        mAuthErrorReadCapWork?.doWork(msg.total)
                                    }
                                }
                            }
                        }

                        override fun failed() {
                            onProgressChanged(100, "检测授权信息失败")
                            callback?.onError(ErrorCode.getErrorResult(-2))
                            onDestroy()
                        }

                    })

            val msg =DataMessageBean(BlastDelegate.getDelegate().getAssemblyCmdLoader().getAuthCycleQueryCmd().cmd)
            BlastDelegate.getDelegate().getCmdExeLoader().exeDelayPollResultCmd(msg.assembly(),BlastDelegate.getDelegate().getQueryDelayTime(),callback)
        }
    }

    override fun cancel() {
        progress = 0
        mAuthErrorReadCapWork?.cancel()
        Receives.getInstance()
            .unRegisterReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader().getAuthCycleQueryCmd())
    }
}